package com.ymq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ymq.reggie.common.R;
import com.ymq.reggie.entity.Employee;
import com.ymq.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(wrapper);

        if(one==null){
            return R.error("登录失败");

        }
        if(!password.equals(one.getPassword())){
            return R.error("密码错误");

        }
        if (one.getStatus()==0){
            return R.error("账号已禁用");

        }
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);

    }
@PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("添加用户: {}",employee.toString());

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
         //employee.setCreateTime(LocalDateTime.now());
        // employee.setUpdateTime(LocalDateTime.now());
       // Long creatID = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(creatID);
      //  employee.setUpdateUser(creatID);

        employeeService.save(employee);
        log.info("新员工：{}",employee);

        return R.success("保存成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        //构造分页查询器
        Page page1 = new Page(page, pageSize);
        //
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        wrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(page1,wrapper);

        return R.success(page1);
    }

    /**
     * 根据empolyee的id修改
     */

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info("员工信息：{}",employee.toString());
       // Long userId= (Long) request.getSession().getAttribute("employee");
       // employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(userId);

        employeeService.updateById(employee);


        return R.success("员工修改成功");
    }

    /**
     * 编辑员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        if(byId!=null){
            return R.success(byId);
        }

        return R.error("修改信息失败");

    }
}
